package com.sypk.exception;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;


public class ErrorCodeManager {
    
	// 用错误码的名字做为key存储
	public final static Map<String, String> CODE_MSG_STORAGE = new HashMap<String, String>();
	// 用错误码的错误代码作为key存储
	public final static Map<String, ErrorCode> CODE_BEAN_STORAGE = new HashMap<String, ErrorCode>();

	public final static String[] filePathArr = new String[]{
		"/exception/errorcode_system.xml",
		"/exception/errorcode_estimate.xml",
	};
	
	static{
		//平台默认的要初始化的文件，减少/config/errorcodemanager.properties配置量
		init();
		//web或者其他自己的缺少的errorcode的文件载入路径配置
		//initByProperites();
	}
	
	/**
	 * 通过异常代码获取异常配置信息
	 */
	public static ErrorCode getErrorCode(String errorCode) {
		 ErrorCode code = CODE_BEAN_STORAGE.get(errorCode);
		 if(code == null){
			 code = new ErrorCode();
			 code.setCode(errorCode);
			 code.setName(errorCode);
			 code.setMsg("unkown");
		 }
		 return code;
	}

	public static String getErrorMsg(String errorCode) {
		 String msg = CODE_MSG_STORAGE.get(errorCode);
		 if(msg == null){
			 return "unkown";
		 }
		 return msg;
	}
	
	public synchronized static void init() {
		
		for(int i=0;i<filePathArr.length;i++){
			init(filePathArr[i]);
		}
	}
	
	public synchronized static void initByProperites() {
		
		InputStream is = ErrorCodeManager.class.getResourceAsStream("/config/errorcodemanager.properties");
		try {
			if(is != null){
				Properties prop = new Properties();
				prop.load(is);
				String fileStr = (String)prop.get("errorcode.file");
				if(fileStr != null && !"".equals(fileStr.trim())){
					String[] fileStrArr = fileStr.split(",");
					if(fileStrArr != null && fileStrArr.length > 0){
						for(int i=0;i<fileStrArr.length;i++){
							init(fileStrArr[i]);
						}
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public synchronized static void init(String filePath) {
		
		InputStream in = null;
		try {
			in = ErrorCodeManager.class.getResourceAsStream(filePath);
			if(in != null){
				Document doc = new SAXReader().read(in);
				Element root = doc.getRootElement();
				List<Node> cfgNodes = root.selectNodes("errorcode");
				if (cfgNodes != null && cfgNodes.size() > 0) {
					for (Node node : cfgNodes) {
						ErrorCode errorCode = new ErrorCode();
	                    errorCode.setCode(node.selectSingleNode("@code").getText());
	                    errorCode.setMsg(node.selectSingleNode("@msg").getText());
	                    errorCode.setName(node.selectSingleNode("@name").getText());
	                    CODE_BEAN_STORAGE.put(errorCode.getCode(), errorCode);
	                    CODE_MSG_STORAGE.put(node.selectSingleNode("@code").getText(), 
	                    		node.selectSingleNode("@msg").getText());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static void main(String[] args) {
        System.out.println(ErrorCodeManager.getErrorCode("0000").getMsg());
    }
}
